import cache from './cache.js' //引入common
import store from './../store'

// 统一跳转
function navigateTo(url) {
	uni.navigateTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
}

// 关闭当前页面并跳转
function redirectTo(url) {
	uni.redirectTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
}

// 切换Tab页面
function switchTab(url) {
	uni.switchTab({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
}

// 加载显示
function loadToShow(msg = '加载中') {
	uni.showToast({
		title: msg,
		icon: 'loading'
	})
}

// 加载隐藏
function loadToHide() {
	uni.hideToast()
}

// 拨打电话
function makePhoneCall(phone) {
	uni.makePhoneCall({
		phoneNumber: phone
	})
}

// 提示框
function showModal(
	title = '提示',
	content = '确认执行此操作吗?',
	callback = () => {},
	showCancel = true,
	cancelText = '取消',
	confirmText = '确定'
) {
	uni.showModal({
		title: title,
		content: content,
		showCancel: showCancel,
		cancelText: cancelText,
		confirmText: confirmText,
		cancelText: cancelText,
		success: function(res) {
			if (res.confirm) {
				// 用户点击确定操作
				setTimeout(() => {
					callback()
				}, 500)
			} else if (res.cancel) {
				// 用户取消操作
			}
		}
	})
}

// 操作成功后，的提示信息
function successToShow(msg = '保存成功', callback = () => {}) {
	setTimeout(() => {
		uni.showToast({
			title: msg,
			icon: 'success',
			duration: 1000,
			success() {
				setTimeout(() => {
					callback()
				}, 500)
			}
		})
	}, 100)
}

// 操作失败的提示信息
function errorToShow(msg = '操作失败', callback = () => {}) {
	setTimeout(() => {
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: 1500,
			success() {
				setTimeout(() => {
					callback()
				}, 1500)
			}
		})
	}, 100)
}

export default {
	navigateTo,
	redirectTo,
	switchTab,
	loadToShow,
	loadToHide,
	makePhoneCall,
	showModal,
	successToShow,
	errorToShow,
}
